/** Copyright (c) Css Team
 * All rights reserved.
 *
 * This file RoleItem.java creation date: [Jan 10, 2014 11:39:51 AM] by liuzhb
 * http://www.css.com.cn
 */
package com.css.apps.base.role.action;

import com.css.apps.base.common.BaseEnvironment;
import com.css.apps.base.func.model.SFunc;
import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.org.model.SUserOrg;
import com.css.apps.base.role.model.SRole;
import com.css.apps.base.role.model.SRoleFunc;
import com.css.apps.base.user.model.SUser;
import com.css.core.model.tree.Tree;
import com.css.core.model.tree.TreeCache;
import com.css.core.model.tree.TreeNode;
import com.css.db.query.QueryCache;
import com.css.util.StringHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * <descption>function infomation</descption>
 * 
 * @author liuzhb
 * @version Jan 10, 2014 11:39:51 AM
 */

public class RoleItem {
	private static Log log = LogFactory.getLog(RoleItem.class);

	@SuppressWarnings("unchecked")
	public static List<String> getSRoleIdList(String userId, String sysId) {
		List<String> list = null;
		try {
			QueryCache qc = new QueryCache("select a.roleId from SUserRole a where a.userId=:userId and a.sysId=:sysId ")
							.setParameter("userId", userId).setParameter("sysId", sysId);
			list = qc.listCache();
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public static List<String> getSRoleFuncIdList(String funcId) {
		List<String> list = null;
		try {
			QueryCache qc = new QueryCache(" select a.uuid from SRoleFunc a where a.funcId=:funcId ")
					.setParameter("funcId", funcId);
			list = qc.list();
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public static Long getSRoleCount() {
		Long roleCount = null;
		try {
			QueryCache qc = new QueryCache("select count(a.uuid) from SRole  a ");
			List<Integer> Ids = qc.listCache();
			if (Ids != null)
				roleCount = (Long) qc.uniqueResult();

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return roleCount;
	}

	@SuppressWarnings("unchecked")
	public static List<SRole> getSRoleBySysId(String sysId) {
		List<SRole> roleList = null;
		try {
			QueryCache qc = new QueryCache("select a.uuid from SRole  a where a.sysId=:sysId ").setParameter("sysId",
					sysId);
			List<Integer> Ids = qc.listCache();
			if (Ids != null)
				roleList = QueryCache.idToObj(SRole.class, Ids);

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return roleList;
	}

	public static boolean getRoleUseFlag(String userId, String roleId) {
		if (StringHelper.isEmpty(userId)) {
			return false;
		}
		SUser sUser = QueryCache.get(SUser.class, userId);
		if (sUser.getRoleList().getListById().contains(roleId)) {
			return true;
		}
		return false;
	}

	public static Long getSRoleCount(String userType) {
		Long roleCount = null;
		try {
			//roleCount = (Long)new QueryCache("select count(a.uuid) from SRole a ").uniqueResult(); 
			StringBuffer ss = new StringBuffer(" select count(a.uuid) from SRole a where 1=1 ");
			// 业务类型用户显示业务类型角色，管理类推
			if (StringHelper.isEmpty(userType) || BaseEnvironment.USERTYPE_NORMAL.equals(userType)) {
				ss.append(" and a.roleType = '" + BaseEnvironment.ROLETYPE_NORMAL + "' ");
			} else {
				ss.append(" and a.roleType = '" + BaseEnvironment.ROLETYPE_ADMIN + "' ");
			}
			roleCount = (Long) new QueryCache(ss.toString()).uniqueResult();
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return roleCount;
	}

	@SuppressWarnings("unchecked")
	public static List<SRoleFunc> getSRoleFuncByRoleId(String roleId) {
		List<SRoleFunc> rolefuncList = null;
		try {
			QueryCache qc = new QueryCache("select a.uuid from SRoleFunc  a where a.roleId=:roleId ").setParameter("roleId", roleId);
			List<Integer> Ids = qc.listCache();
			if (Ids != null)
				rolefuncList = QueryCache.idToObj(SRoleFunc.class, Ids);

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return rolefuncList;
	}

	@SuppressWarnings("unchecked")
	public static JSONArray getSubFunc(String sysId, String roleId) throws JSONException {
		List<String> fids = new QueryCache("select a.funcId from SRoleFunc a where a.roleId=:roleId")
				.setParameter("roleId", roleId).list();
		JSONArray jaTree = new JSONArray();
		String roleType = QueryCache.get(SRole.class, roleId).getRoleType();
		loadSubFunc(sysId, roleType, fids, jaTree);
		return jaTree;
	}

	@SuppressWarnings("unchecked")
	public static JSONArray getFunc(SRole role) throws JSONException {
		List<String> fids = role.getRoleFuncList().getListById();
		JSONArray jaTree = new JSONArray();
		String roleType = role.getRoleType();
		loadSubFunc(role.getSysId(), roleType, fids, jaTree);
		return jaTree;
	}

	private static List<TreeNode> getSubFuncNode(List<String> funcIds) {
		List<TreeNode> lst = null;
		if (funcIds != null && funcIds.size() > 0) {
			lst = new ArrayList<TreeNode>();
			Tree tree = new TreeCache(SFunc.class);
			for (String funcId : funcIds) {
				TreeNode funcNode = tree.getTreeNode(funcId);
				if (funcNode != null) {
					lst.add(funcNode);// add own
					List<TreeNode> children = funcNode.getAllChildren();
					if (children != null && children.size() > 0) {
						lst.addAll(children);
					}
				}

			}
		}
		return lst;
	}

	@SuppressWarnings("unchecked")
	public static void loadSubFunc(String parentId, String roleType, List<?> fids, JSONArray jaTree) throws JSONException {
		StringBuffer sb = new StringBuffer("select a.uuid from SFunc a where a.parentId=:parentId ");
		// 业务类型角色显示业务类型功能，管理类推
		if (StringHelper.isEmpty(roleType) || BaseEnvironment.ROLETYPE_NORMAL.equals(roleType)) {
			sb.append(" and a.funcType = '" + BaseEnvironment.FUNCTYPE_NORMAL + "' ");
		} else {
			sb.append(" and a.funcType = '" + BaseEnvironment.FUNCTYPE_ADMIN + "' ");
		}
		sb.append(" order by a.orderNum ");
		List<String> ids = new QueryCache(sb.toString()).setParameter("parentId", parentId).list();
		List<TreeNode> funcNodes = getSubFuncNode(ids);
		if (funcNodes != null && funcNodes.size() > 0) {
			JSONObject one;
			for (TreeNode funcNode : funcNodes) {
				SFunc func = QueryCache.get(SFunc.class, funcNode.getNodeId());
				if (func != null) {
					boolean auth = false;
					// 业务类型角色显示业务类型功能，管理类推
					if (StringHelper.isEmpty(roleType) || BaseEnvironment.ROLETYPE_NORMAL.equals(roleType)) {
						auth = BaseEnvironment.FUNCTYPE_NORMAL.equals(func.getFuncType());
					} else {
						auth = BaseEnvironment.FUNCTYPE_ADMIN.equals(func.getFuncType());
					}
					if (!auth)
						continue;
					one = new JSONObject();
					one.put("id", func.getUuid());
					one.put("name", func.getName());
					one.put("pId", func.getParentId());
					if (fids.contains(func.getUuid()))
						one.put("checked", true);
					jaTree.add(one);
				}

			}
		}
	}

	@SuppressWarnings("unchecked")
	public static JSONArray getSubUserRole(String userId, String sysId) throws JSONException {
		List<String> jsids = new QueryCache("select a.roleId from SUserRole a where a.userId=:userId")
				.setParameter("userId", userId).listCache();
		List<String> ids = new QueryCache("select a.uuid from SRole a where a.sysId=:sysId and a.openFlag='1'")
				.setParameter("sysId", sysId).listCache();
		List<SRole> lst = QueryCache.idToObj(SRole.class, ids);
		JSONArray jaTree = new JSONArray();
		JSONObject one;
		if (lst != null)
			for (SRole item : lst) {
				one = new JSONObject();
				one.put("id", item.getUuid());
				one.put("name", item.getName());
				one.put("pId", "0");
				one.put("nocheck", false);
				one.put("isParent", false);
				if (jsids.contains(item.getUuid())) {
					one.put("chkDisabled", true);
					one.put("checked", true);
				}
				jaTree.add(one);
			}
		return jaTree;
	}

	public static JSONObject getRootUserRole() throws JSONException {// 10
		JSONObject one = new JSONObject();
		one.put("id", "0");
		one.put("name", "所有角色");
		one.put("pId", "00");
		one.put("isParent", true);
		one.put("nocheck", true);
		return one;
	}

	// 机构树不显示orgId节点
	public static JSONArray getAllOrgUserTree(String userIdList) throws JSONException {// 10
		Tree tree = new TreeCache(SOrg.class);
		TreeNode rootNode = tree.getRoot().get(0);
		List<TreeNode> lst = new ArrayList<TreeNode>();
		lst.add(rootNode);
		lst.addAll(rootNode.getAllChildren());
		JSONObject one;
		JSONArray orgTree = new JSONArray();
		for (TreeNode o : lst) {
			SOrg item = QueryCache.get(SOrg.class, o.getNodeId());
			if (item == null)
				continue;
			one = new JSONObject();
			one.put("id", item.getUuid());
			one.put("name", item.getName());
			one.put("pId", item.getParentId());
			one.put("isParent", true);
			one.put("type", "org");
			orgTree.add(one);
		}
		/*for (TreeNode o : lst) {
			orgTree.addAll(getUserJsonByOrgIdList(o.getNodeId(), userIdList));
		}*/
		return orgTree;
	}

	@SuppressWarnings("unchecked")
	public static JSONArray getUserJsonByOrgIdList(String orgId, String userIdList) {
		JSONArray jaTree = new JSONArray();
		List<String> listt = new QueryCache("select a.uuid from SUserOrg  a where a.deptId=:orgId ")
				.setParameter("orgId", orgId).list();
		List<SUserOrg> list = QueryCache.idToObj(SUserOrg.class, listt);
		if (list != null) {
			for (SUserOrg uo : list) {
				SUser u = QueryCache.get(SUser.class, uo.getUserId());
				if (u != null && !userIdList.contains(u.getUuid())
						&& BaseEnvironment.USERTYPE_NORMAL.equals(u.getUserType())) {
					JSONObject one = new JSONObject();
					one.put("id", u.getUserId());
					one.put("name", u.getRealName());
					one.put("pId", orgId);
					one.put("type", "user");
					one.put("isParent", false);
					jaTree.add(one);
				}
			}
		}
		return jaTree;
	}

}
